from utils import date_util


# 完成交易方法
# ave_flag = 0  # 0:无动作 1:买入 -1 卖出
# macd_flag = 0  # 0:无动作 1:买入 -1 卖出
# dif_flag = 0  # 0:无动作 1:买入 -1 卖出
# macd_flag2 = 0  # 0:无动作 1:买入 -1 卖出
# dea_flag2 = 0  # 0:无动作 1:买入 -1 卖出
# dif_flag2 = 0  # 0:无动作 1:买入 -1 卖出

def deal_complete(data):
    data['bs'] = 0
    deal_price = data['rt_price']
    deal_flag = data['deal_flag']
    capital = data['capital']
    hand = data['hand']
    trigger_num = data['trigger_num']
    # 是否有触发
    ave_flag = data['ave_flag']
    macd_flag = data['macd_flag']
    dif_flag = data['dif_flag']
    macd_flag2 = data['macd_flag2']
    dea_flag2 = data['dea_flag2']
    dif_flag2 = data['dif_flag2']
    if deal_flag == 0:
        return data
    if ave_flag == 0 and macd_flag == 0 and dif_flag == 0 \
            and macd_flag2 == 0 and dea_flag2 == 0 and dif_flag2 == 0:
        return data

    if 'last_deal_date' in data.keys() and data['last_deal_date'] == data['rq']:
        print("不符合 T+1 交易规则")
        return data

    score = data['score']
    level = data['score_info']['level']

    # buy_sell_flag = 0  # 1买入 2卖出
    # 交易标志 deal_flag = 0  # 1:全仓 2:加半仓 3:清仓 4:减半仓
    if deal_flag == 1 or deal_flag == 2:  # 买入
        if level == '青铜':
            print('青铜选手不买入' + str(score))
            return data
        buy_sell_flag = 1
        deal_capital = capital
        if hand == 0 and deal_flag == 2:
            if level == '星耀' or level == '王者':
                deal_capital = capital
            elif level == '钻石':
                deal_capital = capital
            else:
                deal_capital = int(capital / 2)
        else:
            if level == '白银':
                deal_capital = int(capital)
            # elif level == '黄金':
            #     deal_capital = int(capital * 2 / 3)
        deal_hand = int(deal_capital / (deal_price * 100))
        if deal_hand == 0:  # 如果可购买数量为0 返回结果
            return data
    elif deal_flag == 3 or deal_flag == 4:  # 卖出
        if level == '星耀' or level == '王者':
            print('星耀/王者选手不卖出')
            return data
        buy_sell_flag = -1
        if hand != 0:
            deal_hand = int(capital / (deal_price * 100))
            if deal_hand == 0 and deal_flag == 4:  # 全仓状态
                if level == '青铜':
                    deal_hand = hand
                elif level == '白银':
                    deal_hand = int(hand * 2 / 3)
                else:
                    deal_hand = int(hand / 2)
            else:  # 清仓
                deal_hand = hand
        else:
            return data
    else:
        return data
    hand = hand + deal_hand * buy_sell_flag
    deal_amt = round(deal_hand * deal_price * 100, 2)  # 成交金额
    deal_tax = round(deal_amt * 0.00025, 2)  # 印花税
    data['bs'] = buy_sell_flag
    data['hand'] = hand
    data['position'] = hand * deal_price * 100
    data['capital'] = capital - deal_amt * buy_sell_flag - deal_tax
    data['trigger_num'] = trigger_num + 1
    data['last_deal_date'] = data['rq']
    return data


def deal_complete_v1(data):
    data['bs'] = 0  # 重置bs值
    ave_flag = data['ave_flag']
    macd_flag = data['macd_flag']
    dif_flag = data['dif_flag']
    macd_flag2 = data['macd_flag2']
    dea_flag2 = data['dea_flag2']
    dif_flag2 = data['dif_flag2']
    if ave_flag == 0 and macd_flag == 0 and dif_flag == 0 and macd_flag2 == 0 and dea_flag2 == 0 and dif_flag2 == 0:
        return data
    rt_price = data['rt_price']
    capital = data['capital']  # 剩余闲置资金
    hand = data['hand']
    trigger_num = data['trigger_num']
    old_score = 0
    if 'deal_score' in data.keys():
        old_score = data['deal_score']
    new_score = data['score_info']['score']
    if data['rq'] == '2020-11-17' and new_score == 55:
        position = round(hand * rt_price * 100, 2)
        print(position)
    if new_score <= 0:
        bs_flag = -1
    elif new_score < old_score:
        bs_flag = -1
    else:
        bs_flag = 1
    # 拦截不必要的规则
    if new_score < 0 and old_score < 0:
        print("都是清仓不需要调整")
        return data
    if abs(abs(new_score) - abs(old_score)) < 25:
        print("波动太小不需要调整")
        return data
    if 'last_deal_date' in data.keys() and data['last_deal_date'] == data['rq']:
        print("不符合 T+1 交易规则")
        return data
    if bs_flag == 1 and new_score < 20:
        print("买入意愿不强烈")
        return data
    data['last_deal_date'] = data['rq']
    data['bs'] = bs_flag  # 重新bs赋值
    data['deal_score'] = new_score  # 更新交易评分
    position = round(hand * rt_price * 100, 2)
    if bs_flag == -1 and new_score < 0:
        need_p = 0
    else:
        need_p = (capital + position) * new_score / 100
    deal_money = need_p - position
    deal_hand = int(deal_money / (rt_price * 100))
    hand = hand + deal_hand
    deal_amt = abs(round(deal_hand * rt_price * 100, 2))  # 成交金额
    deal_tax = round(deal_amt * 0.00025, 2)  # 印花税
    data['hand'] = hand
    data['position'] = hand * rt_price * 100
    data['capital'] = capital - deal_amt * data['bs'] - deal_tax
    data['trigger_num'] = trigger_num + 1
    return data
